package com.citcc.common.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.lang3.text.StrBuilder;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 * 
 * @author xiongzai
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    
    static SimpleDateFormat sdf_ymdhms_line = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat sdf_ymdhms = new SimpleDateFormat("yyyyMMddHHmmss");
	static SimpleDateFormat sdf_ymd_line = new SimpleDateFormat("yyyy-MM-dd");
	static SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyyMMdd");
	static SimpleDateFormat sdf_hms = new SimpleDateFormat("HH:mm:ss");
    
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
    
    public static String get3MinAgo() {
        Calendar cal = Calendar.getInstance();
        //原3 
        cal.add(Calendar.MINUTE,-2);
        String date = sdf.format(cal.getTime());
        return date;
    }
    
    /**
	 * 字符串转换为date
	 * type==1 2018-11-19;type==2 2018-11-19 14:50:45
	 * type==3 20181119;type==4 20181119145045
	 * @param dateStr
	 * @param type
	 * @return
	 */
	public static Date formatStr2Date(String dateStr, int type){
		SimpleDateFormat sdf = null;
		switch (type) {
		case 1:
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			break;
		case 2:
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		case 3:
			sdf = new SimpleDateFormat("yyyyMMdd");
			break;
		case 4:
			sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			break;
		default:
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		}
		Date date = null;
		try {
			date= sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
		
	}
	
	/**
	 * type==1yyyy-MM-dd HH:mm:ss
	 * type==2yyyy-MM-dd
	 * type==3HH:mm:ss
	 * type==4yyyyMMddHHmmss
	 * type==5yyyyMMdd
	 * @param sdfType
	 * @return
	 */
	public static String formatDate2Str(int sdfType, Date date){
		String result = "";
		switch (sdfType) {
		case 1:
			result = sdf_ymdhms_line.format(date);
			break;
		case 2:
			result = sdf_ymd_line.format(date);
			break;
		case 3:
			result = sdf_hms.format(date);
			break;
		case 4:
			result = sdf_ymdhms.format(date);
			break;
		case 5:
			result = sdf_ymd.format(date);
			break;
		default:
			break;
		}
		return result;
	}
    /**
     * 把毫秒数转换成时分秒
     * @param millis
     * @return
     */
    public static String millisToStringShort(long millis) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//这里想要只保留分秒可以写成"mm:ss"
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String hms = formatter.format(millis);
        System.out.println(hms);
        return hms;
    }

    /**
     * 把毫秒数转换成时分秒
     * @param millis
     * @return
     */
    public static String millisToStringShortCN(long millis) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH时mm分ss秒");//这里想要只保留分秒可以写成"mm:ss"
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String hms = formatter.format(millis);
        System.out.println(hms);
        return hms;
    }

    public static void main(String[] args) {
        String s = millisToStringShortCN(100000l);
        System.out.println(s);
    }
}
